﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bunian.Core.BusinessObjects;
//using Iesi.Collections.Generic;

namespace Bunian.Web.Pages
{
    namespace Bunian
    {
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Do nothing
            }

            protected void btnTest_Click(object sender, EventArgs e)
            {
                //TestFamilyChildHierarchy();
                //TestOrphanBeneficiaryHierarchy();
                //TestGetOrphan();
                //TestIBeneficiary();

                //TestSponsorships();

                //TestLoadSponsorships();
            }

            private void TestLoadSponsorships()
            {
                //Sponsor x = Sponsor.Repository.Get(4);
                //Tender x = Tender.Repository.Get(6);

                //IBeneficiary ben = Beneficiary.Repository.Get(3);

                //foreach (Sponsorship y in ben.BeneficiaryTenders.First<Tender>().Sponsorships)
                //{
                //    Response.Write(y.Amount);
                //}
            }

            private void TestSponsorships()
            {
                //Sponsor spon = new Sponsor();

                //Contact con = new Contact();

                //con.BirthDate = DateTime.ParseExact("25/6/1982", "d/M/yyyy", null);
                //con.FullName = "Tamim Al Manaseer";
                //con.Sex = Gender.Male;
                //con.IsOrganization = false;

                //spon.ContactInfo = con;

                //spon.Sponsorships = new SortedSet<Sponsorship>();

                //Sponsorship s = new Sponsorship();

                //s.Amount = 100;
                //s.StartDate = DateTime.Now;
                //s.Status = SponsorshipStatus.Proposed;
                //s.Type = SponsorshipType.Single;
                //s.SponsorInfo = spon;
                ////s.BeneficiaryTender = Tender.Repository.Get(6);

                //spon.Sponsorships.Add(s);

                //spon.Save();
            }

            private void TestGetOrphan()
            {
                //Child c = Child.Repository.Get(3);
                //For some reason NHibernate messed up returning the Tender as Orphan, instead it returns it as an unknown proxy type.
                //Disabling lazy loading solved this issue. also loading the Orphan directly solved the issue in subsequent calls.
                //I dont know the reason so far, but i kept the solution of having IBeneficiary Eagerly Loaded
                //Orphan oph = (Orphan)Tender.Repository.Get(6);

                //Response.Write(((Child)(c.BeneficiaryTenders.First<Tender>().Beneficiary)).ContactInfo.FullName);
                //Response.Write(oph.ChildInfo.ContactInfo.FullName);
            }

            private static void TestIBeneficiary()
            {
                //IBeneficiary x = Beneficiary.Repository.Get(7);

            }

            private static void TestOrphanBeneficiaryHierarchy()
            {
                //Contact con = new Contact();

                //con.BirthDate = DateTime.ParseExact("25/4/1982", "d/M/yyyy", null);
                //con.FullName = "Tamim Salem";
                //con.Sex = Gender.Male;
                //con.IsOrganization = false;

                //Child chld = Child.Repository.Get(3);//new Child();

                //chld.ContactInfo = con;

                Orphan orph = new Orphan();

                //orph.ChildInfo = chld;

                //chld.BeneficiaryTenders = new SortedSet<Tender>();
                //chld.BeneficiaryTenders.Add(orph);

                //orph.Beneficiary = chld;

                //chld.Save();
            }

            private static void TestFamilyChildHierarchy()
            {
                //Bunian.Core.BusinessObjects.Contact con = new Contact();

                //con.BirthDate = DateTime.ParseExact("25/4/1982", "d/M/yyyy", null);
                //con.FullName = "Tamim Salem";
                //con.Sex = Gender.Male;
                //con.IsOrganization = false;

                //Contact con1 = new Contact();
                //con1.BirthDate = DateTime.ParseExact("1/4/1982", "d/M/yyyy", null);
                //con1.FullName = "Emad Ashi";
                //con1.Sex = Gender.Female;
                //con1.IsOrganization = false;

                //Contact con2 = new Contact();
                //con2.BirthDate = DateTime.ParseExact("16/4/1982", "d/M/yyyy", null);
                //con2.FullName = "Ahmad Ashi";
                //con2.Sex = Gender.Male;
                //con2.IsOrganization = false;

                //Family fam = new Family();

                //fam.Father = con;
                //fam.Provider = con2;
                //fam.ProviderKinship = Kinship.GrandfatherFatherSide;


                //fam.FamilyMembers = new Iesi.Collections.Generic.SortedSet<Child>();

                //Child chld = new Child();

                //chld.ContactInfo = con1;

                //fam.FamilyMembers.Add(chld);
                //chld.Family = fam;

                //fam.BeneficiaryTenders = new Iesi.Collections.Generic.SortedSet<Tender>();

                //Tender ten = new Tender();

                ////ten.Type = TenderType.Repository.Get(1);

                //fam.BeneficiaryTenders.Add(ten);
                //ten.Beneficiary = fam;

                ////fam.Save();
            }
        }
    }
}